package com.crane.oss.sports;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.crane.oss.xunfei.req.XunFeiApiRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.crane.common.core.utils.Md5Util;
import com.crane.common.framework.utils.BeanUtil;

@Component
public class XunFeiApiService {

//    @Resource
//    XunFeiAppConfig xunFeiAppConfig;

    XunFeiAppConfig xunFeiAppConfig=new XunFeiAppConfig();
     //"csezpid"


    public Map<String, String> getStringStringMap(XunFeiApiRequest request, String sid, String did, String uid,String... ignoredProps) {
        request.setSid(sid);
        request.setAppKey(xunFeiAppConfig.getAppKey());
        request.setF("iflytek");
        request.setUid(uid);
        request.setDid(did);
        request.setDt(System.currentTimeMillis());
        List<String> nullParamList = new ArrayList<>(Arrays.asList(ignoredProps));
        Map<String, String> params= BeanUtil.asStringMap(request);
        StringBuilder sb = new StringBuilder();
        params.keySet().stream().sorted().collect(Collectors.toList())
                .forEach(key -> {
                    if (!nullParamList.contains(key) && !StringUtils.isEmpty(params.get(key))) {
                        sb.append(key).append("=").append(params.get(key)).append("&");
                    }
                });
        sb.append(xunFeiAppConfig.getAppSecret());
        String md5sign =Md5Util.stringToMD5(sb.toString());
        params.put("sign",md5sign);
        return params;
    }
}

